package com.itheima.web;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Orders;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("支付成功");
    }

    /**
     * 分页查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(Integer page,Integer pageSize){
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        orderService.page(ordersPage);
        log.info("订单分页数据{}",ordersPage.getRecords());
        return R.success(ordersPage);
    }

    /**
     * 管理端分页查询订单数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize,String number,String beginTime,String endTime){
        log.info(number);
        log.info(beginTime);
        log.info(endTime);
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        lqw.between(StringUtils.isNotEmpty(beginTime)&&StringUtils.isNotEmpty(endTime),Orders::getOrderTime,beginTime,endTime);
        orderService.page(ordersPage,lqw);
        log.info("订单分页数据{}",ordersPage.getRecords());
        return R.success(ordersPage);
    }

    /**
     * 分页查询以excel形式下载
     * @param beginTime
     * @param endTime
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void download(String beginTime,String endTime,HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = UUID.randomUUID().toString();
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.between(StringUtils.isNotEmpty(beginTime)&&StringUtils.isNotEmpty(endTime),Orders::getOrderTime,beginTime,endTime);
        List<Orders> orders = orderService.list(lqw);
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),Orders.class).sheet("模板").doWrite(orders);
    }
}
